package com.example.demo.controller;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.entity.Administrators;
import com.example.demo.repository.AdministratorsRepository;
import com.example.demo.util.CommonUtil;

@RestController
@CrossOrigin
public class AdministratorsRepositoryController {
	@Autowired
	private AdministratorsRepository administratorsRepository;
	/**
	 * 	添加管理员信息
	 * @param administrators
	 * @return 
	 */
	@PostMapping("/AddAdministrators")
	public Map<String, Object> AddAdministrators(Administrators administrators){
		Map<String, Object> map = CommonUtil.getResultMap();
		Administrators admin = administratorsRepository.save(administrators);
		if(admin!=null) {
			map.put("success", true);
			map.put("message", "添加成功!");
		}else {
			map.put("success", false);
			map.put("message", "添加失败!");
		}
		return map;
	}
	
	@PostMapping("/FindAllAdministrators")
	public Map<String, Object> FindAllAdministrators(Administrators administrators){
		Map<String, Object> map = CommonUtil.getResultMap();
		Administrators findByAdminNameAndAdminPassword = administratorsRepository.findByAdminName(administrators.getAdminName());
		if(findByAdminNameAndAdminPassword!=null) {
			if(findByAdminNameAndAdminPassword.getAdminPassword().equals(administrators.getAdminPassword())) {
				map.put("success", true);
				map.put("message", "登录成功!");
			}else {
				map.put("success", false);
				map.put("message", "输入密码错误!");
			}
		}else {
			map.put("success", false);
			map.put("message", "用户不存在!");
		}
		
		return map;
	}
	
	
}
